<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
    // promise 实例对象
    // let p = new Promise((resolve, reject) => {
    //     reject('失败');
    // })
    // p.then(res => {
    //     console.log(res);
    // }, err => {
    //     console.log(err);
    // }) 

    // p.then(res => {
    //     console.log(res);
    // }).catch(err => {
    //     console.log(err, 'catch');
    // }) 
    // 多个promise 对象 执行 
    // 1.catch :捕捉 promise 链上的错误 ； 一旦错误，后面代码不执行
    function fn1() {
        return new Promise((resolve, reject) => {
            setTimeout(function() {
                resolve(111)
            },1000)
        })
    }
    function fn2() {
        return new Promise((resolve, reject) => {
            setTimeout(function() {
                // resolve(222)
                reject('错误')
            },1000)
        })
    }
    function fn3() {
        return new Promise((resolve, reject) => {
            setTimeout(function() {
                resolve(333)
            },1000)
        })
    }
    fn1().then(res => {
        console.log(res);
        return fn2();
    }).then(res => {
        console.log(res);
        return fn3();
    }).then(res => {
        console.log(res);
    }).catch(res => {
        console.log(res, 'catch...');
    })

    async function fn() {
        try {
            await fn2();
        } catch (error) {
            console.log(error, 'try');
        }
        
    }
    fn2().finally(() => {
        console.log('执行完了');
    })
    </script>
</body>
</html>